
Imports System.Data
Imports System.Data.SqlClient


Namespace Kardex
Public Class Kardex

public Function Listar(KardexId as integer) as Entity.Kardex.Kardex

Dim strNameProcedure as string = "up_Kar_Kardex_obtener"
Dim sqlReader As SqlDataReader = Nothing
Dim sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
dim objEnt as   entity.Kardex.Kardex
Try
            sqlCon.Open()
Dim sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
.CommandType = CommandType.StoredProcedure
                .Parameters.Add(New SqlParameter("@KardexId", SqlDbType.Int)).Value =KardexId
            End With
            sqlReader = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
If sqlReader.HasRows Then
While sqlReader.Read
 objEnt= new    entity.Kardex.Kardex
With objEnt
		.KardexId = sqlReader("KardexId").ToString
		.ProductoId = sqlReader("ProductoId").ToString
		.ALmacenId = sqlReader("ALmacenId").ToString
		.TipoId = sqlReader("TipoId").ToString
		.Cantidad = sqlReader("Cantidad").ToString
		.CantidadIn = sqlReader("CantidadIn").ToString
		.CantidadOut = sqlReader("CantidadOut").ToString
		.Total = sqlReader("Total").ToString
		.Fecha = sqlReader("Fecha").ToString
		.TipoMovimientoId = sqlReader("TipoMovimientoId").ToString
		.Referencia = sqlReader("Referencia").ToString
End With
  End While
  End IF
 Catch ex As Exception
Throw New Exception(ex.ToString)
Finally
 sqlReader.Dispose()
  sqlReader.Close()
  sqlReader = Nothing
  sqlCon.Dispose()
 sqlCon.Close()
 sqlCon = Nothing
 End Try
return objEnt

End Function


'***************************



public Function Listar(Opcion as string , _
		KardexId as Integer, _
		ProductoId as Integer, _
		ALmacenId as String, _
		TipoId as Integer, _
		Cantidad as Integer, _
		CantidadIn as Integer, _
		CantidadOut as Integer, _
		Total as Integer, _
		Fecha as Long, _
		TipoMovimientoId as Integer, _
		Referencia as Integer) _
as  Entity.Kardex.KardexCol

Dim strNameProcedure as string = "up_Kar_Kardex_listar"
dim objCol as   entity.Kardex.KardexCol
Try
Using conn As New SqlConnection(Configuracion.Connexion.Cnn)
Dim cmd As New SqlCommand(strNameProcedure, conn)
cmd.CommandType = CommandType.StoredProcedure
  cmd.Parameters.Add(New SqlParameter("@opcion", SqlDbType.varchar,50)).Value =Opcion
 cmd.Parameters.Add(New SqlParameter("@KardexId",SqlDbType.INT)).Value =KardexId
 cmd.Parameters.Add(New SqlParameter("@ProductoId",SqlDbType.INT)).Value =ProductoId
 cmd.Parameters.Add(New SqlParameter("@ALmacenId",SqlDbType.NCHAR,20)).Value =ALmacenId
 cmd.Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 cmd.Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.INT)).Value =Cantidad
 cmd.Parameters.Add(New SqlParameter("@CantidadIn",SqlDbType.INT)).Value =CantidadIn
 cmd.Parameters.Add(New SqlParameter("@CantidadOut",SqlDbType.INT)).Value =CantidadOut
 cmd.Parameters.Add(New SqlParameter("@Total",SqlDbType.INT)).Value =Total
 cmd.Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 cmd.Parameters.Add(New SqlParameter("@TipoMovimientoId",SqlDbType.INT)).Value =TipoMovimientoId
 cmd.Parameters.Add(New SqlParameter("@Referencia",SqlDbType.INT)).Value =Referencia
conn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
		dim iKardexId as integer = dr.GetOrdinal("KardexId")
		dim iProductoId as integer = dr.GetOrdinal("ProductoId")
		dim iALmacenId as integer = dr.GetOrdinal("ALmacenId")
		dim iTipoId as integer = dr.GetOrdinal("TipoId")
		dim iCantidad as integer = dr.GetOrdinal("Cantidad")
		dim iCantidadIn as integer = dr.GetOrdinal("CantidadIn")
		dim iCantidadOut as integer = dr.GetOrdinal("CantidadOut")
		dim iTotal as integer = dr.GetOrdinal("Total")
		dim iFecha as integer = dr.GetOrdinal("Fecha")
		dim iTipoMovimientoId as integer = dr.GetOrdinal("TipoMovimientoId")
		dim iReferencia as integer = dr.GetOrdinal("Referencia")
		dim i_usu_cre as integer = dr.GetOrdinal("_usu_cre")
		dim i_fec_cre as integer = dr.GetOrdinal("_fec_cre")
		dim i_usu_mod as integer = dr.GetOrdinal("_usu_mod")
		dim i_fec_mod as integer = dr.GetOrdinal("_fec_mod")
		dim i_flg_eli as integer = dr.GetOrdinal("_flg_eli")

 Dim colCount As Integer = dr.FieldCount
 Dim values As Object() = New Object(colCount - 1) {}

 objCol =  New entity.Kardex.KardexCol
  If dr.HasRows Then
While dr.Read()
dim objEnt =  new entity.Kardex.Kardex
dr.GetValues(values)

		objEnt.KardexId = values(iKardexId)
		objEnt.ProductoId = values(iProductoId)
		objEnt.ALmacenId = values(iALmacenId)
		objEnt.TipoId = values(iTipoId)
		objEnt.Cantidad = values(iCantidad)
		objEnt.CantidadIn = values(iCantidadIn)
		objEnt.CantidadOut = values(iCantidadOut)
		objEnt.Total = values(iTotal)
		objEnt.Fecha = values(iFecha)
		objEnt.TipoMovimientoId = values(iTipoMovimientoId)
		objEnt.Referencia = values(iReferencia)
 objCol.Add(objEnt)
 End While
 End if
 End Using
 End Using
 Catch ex As Exception
Throw New Exception(ex.ToString)
Finally
 End Try
return objCol

End Function


'***************************


Public  Function Insert(ByVal opcion As String, _
		KardexId as Integer, _
		ProductoId as Integer, _
		ALmacenId as String, _
		TipoId as Integer, _
		Cantidad as Integer, _
		CantidadIn as Integer, _
		CantidadOut as Integer, _
		Total as Integer, _
		Fecha as Long, _
		TipoMovimientoId as Integer, _
		Referencia as Integer, _
vUsuario as integer ) As Integer

 dim strNameProcedure as string ="Up_Kar_Kardex_Insertar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
 .Parameters.Add(New SqlParameter("@KardexId",SqlDbType.INT)).Value =KardexId
 .Parameters.Add(New SqlParameter("@ProductoId",SqlDbType.INT)).Value =ProductoId
 .Parameters.Add(New SqlParameter("@ALmacenId",SqlDbType.NCHAR,20)).Value =ALmacenId
 .Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 .Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.INT)).Value =Cantidad
 .Parameters.Add(New SqlParameter("@CantidadIn",SqlDbType.INT)).Value =CantidadIn
 .Parameters.Add(New SqlParameter("@CantidadOut",SqlDbType.INT)).Value =CantidadOut
 .Parameters.Add(New SqlParameter("@Total",SqlDbType.INT)).Value =Total
 .Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 .Parameters.Add(New SqlParameter("@TipoMovimientoId",SqlDbType.INT)).Value =TipoMovimientoId
 .Parameters.Add(New SqlParameter("@Referencia",SqlDbType.INT)).Value =Referencia
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************


Public  Function Update(ByVal opcion As String, _
		KardexId as Integer, _
		ProductoId as Integer, _
		ALmacenId as String, _
		TipoId as Integer, _
		Cantidad as Integer, _
		CantidadIn as Integer, _
		CantidadOut as Integer, _
		Total as Integer, _
		Fecha as Long, _
		TipoMovimientoId as Integer, _
		Referencia as Integer, _
vUsuario as integer ) As Integer

 dim strNameProcedure as string ="Up_Kar_Kardex_Actualizar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
 .Parameters.Add(New SqlParameter("@KardexId",SqlDbType.INT)).Value =KardexId
 .Parameters.Add(New SqlParameter("@ProductoId",SqlDbType.INT)).Value =ProductoId
 .Parameters.Add(New SqlParameter("@ALmacenId",SqlDbType.NCHAR,20)).Value =ALmacenId
 .Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 .Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.INT)).Value =Cantidad
 .Parameters.Add(New SqlParameter("@CantidadIn",SqlDbType.INT)).Value =CantidadIn
 .Parameters.Add(New SqlParameter("@CantidadOut",SqlDbType.INT)).Value =CantidadOut
 .Parameters.Add(New SqlParameter("@Total",SqlDbType.INT)).Value =Total
 .Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 .Parameters.Add(New SqlParameter("@TipoMovimientoId",SqlDbType.INT)).Value =TipoMovimientoId
 .Parameters.Add(New SqlParameter("@Referencia",SqlDbType.INT)).Value =Referencia
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************


Public Shared Function Eliminar(ByVal opcion As String, KardexId as integer, vusuario as integer) as integer

 dim strNameProcedure as string ="Up_Kar_Kardex_Eliminar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
                .Parameters.Add(New SqlParameter("@KardexId", SqlDbType.Int)).Value =KardexId
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************



End Class



end namespace

